
import java.util.ArrayList;
import java.util.Random;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Afshin
 */
public class AgentDispatcher {

    public ArrayList<Agent> generateAgents(int nAgents) {
        TextReader tr = new TextReader();
        ArrayList<String> FirstNames = tr.getStrings("src/gData/FirstNames.txt");
        ArrayList<String> LastNames = tr.getStrings("src/gData/LastNames.txt");
        ArrayList<String> LikeDislike = tr.getStrings("src/gData/likesdislikes.txt");
        ArrayList<Agent> agents = new ArrayList<Agent>();
        Random r = new Random();
        //Initialize Statics in Agents
        for (int i = 0; i < nAgents; i++) {
            Agent ag = new Agent(i);
            
            ArrayList<String> likes = new ArrayList<String>();
            ArrayList<String> dislikes = new ArrayList<String>();
            ArrayList<Event> myEvs = new ArrayList<Event>();
            ArrayList<Integer> nSeen = new ArrayList<Integer>();
            int a = r.nextInt(53);
            int b = r.nextInt(53);
            double x = r.nextDouble()*150 * (r.nextBoolean() ? -1 : 1);
            double y = r.nextDouble()*150 * (r.nextBoolean() ? -1 : 1);
            String name = FirstNames.get(a)+" "+LastNames.get(b);
            //System.out.println(name);
            //System.out.println("X="+x);
            //System.out.println("Y="+y);
            ag.setAgentname(name);
            ag.setLocx(x);
            ag.setLocy(y);
            ag.setMyEvents(myEvs);
            ag.setIsLiving(true);
            ag.setIsKiller(false);
            for(int j=0; j<4;j++){
            int c = r.nextInt(25);
            int d = r.nextInt(25);
            while(c==d){
            c = r.nextInt(25);
            d = r.nextInt(25);
            }
            while((nSeen.contains(c) || nSeen.contains(d)) || (c==d)){
               //System.out.println("REPETITION ALERT");
               c = r.nextInt(25);
               d = r.nextInt(25);
            }
            nSeen.add(c);
            nSeen.add(d);
            //System.out.print(""+LikeDislike.get(c)+" , ");
            likes.add(LikeDislike.get(c));
            dislikes.add(LikeDislike.get(d));
            //System.out.print(" !"+LikeDislike.get(d)+" , ");
            }
            ag.setLikes(likes);
            ag.setDislikes(dislikes);
            agents.add(ag);
            //System.out.println("nseen="+nSeen.toString());
        }
        int maxEn = 0; 
        int index = 0;
        for(int i=0; i<agents.size();i++){
        ArrayList<Integer> friends = new ArrayList<Integer>();
        ArrayList<Integer> enemies = new ArrayList<Integer>();
        for(int j=0; j<agents.size();j++){
        	if (i == j) {
        		continue; // so that we're not friends with ourselves.
        	}
        for(int k=0; k<agents.get(i).likes.size(); k++){
            String clike = agents.get(i).getLikes().get(k);
            if(agents.get(j).getLikes().contains(clike) && !friends.contains(j)){
                friends.add(j);
            }
        }
            if(!friends.contains(j)){
                enemies.add(j);
            }
        }
        if(enemies.size() > maxEn){
        index = i;
        maxEn = enemies.size();
        }
        System.out.println(agents.get(i).getAgentname());
        //System.out.println("Friends"+friends.toString());
        //System.out.println("Enemies"+enemies.toString());
        agents.get(i).setEnemies(enemies);
        agents.get(i).setFriends(friends);
        }
        System.out.println("Our candidate for killer is "+agents.get(index).getAgentname());
        System.out.println("The killer does not like : "+agents.get(index).getDislikes().toString());
        System.out.println("This is the hit-list "+agents.get(index).getEnemies().toString());
        agents.get(index).setIsKiller(true);
        
        
        return agents;
    }
    
    
    public boolean isGameOver(Agent killer) {
        boolean isGameOver = true;
        for(int i = 0; i < killer.enemies.size(); i++){
        if(Global.agents.get(killer.enemies.get(i)).isLiving){
        isGameOver = false;
        }
        }
        return isGameOver;
    }
    
    
}
